package com.houdoudou.supplier.fragment;

import java.util.Map;

import com.houdoudou.supplier.R;
import com.houdoudou.supplier.bean.Sort;
import com.houdoudou.supplier.bean.Supplier;
import com.houdoudou.supplier.common.CommonFunction;
import com.houdoudou.supplier.common.Constant;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;


import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class IndexBasicFragment extends Fragment{
	String affair="";
    JSONObject indexJO= new JSONObject();
	EditText id,name,show_no;
	TextView result,title;
	View.OnClickListener myOnclickListener;
	Button confirm;
	ProgressBar progres;


	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		 View view =inflater.inflate(R.layout.frg_indexbasic, container, false);
           init_listener();
           init_component(view);
           assign();
		 return view;
	}
	@Override
	public void setArguments(Bundle bundle) {
		// TODO Auto-generated method stub
		// super.setArguments(args);
		affair=bundle.getString("affair");
		if(affair.equals("updateIndex")){
			indexJO= JSONObject.fromObject(bundle.getString("index"));
		}
	}
  
	@Override
	public void onHiddenChanged(boolean hidden) {
		// TODO Auto-generated method stub
		super.onHiddenChanged(hidden);
		if(!hidden){
			System.out.println("affair="+affair);
			assign();
		}
	}
	
	private void assign(){
		if(affair.equals("createIndex")) {
			title.setText("新增分类");
			id.setText("");
			name.setText("");
			show_no.setText("");
		}else if(affair.equals("updateIndex")){			
			title.setText("修改分类");
			id.setText(indexJO.getInt(Sort.ID)+"");
			name.setText(indexJO.getString(Sort.NAME));
			show_no.setText(indexJO.getInt(Sort.DISPLAY)+"");
		}
	}
	private void init_listener(){
		
		
		myOnclickListener= new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				result.setText("");
				boolean check=true;		
				int showno_value=100;
				if( name.getText().toString().length()>5){
					 new AlertDialog.Builder(getActivity()).setTitle("输入错误提示")
					 .setMessage("名称不能超过5个字").setPositiveButton("确定", null).show();
					 check=false;
					 
					}else{
					indexJO.put(Sort.NAME, name.getText().toString());
					    }
				try{			
					showno_value=Integer.parseInt(show_no.getText().toString());
					   indexJO.put(Sort.DISPLAY, showno_value);
				}catch(Exception e){
					e.printStackTrace();
					check=false;
					 new AlertDialog.Builder(getActivity()).setTitle("错误提示")
		    		 .setMessage("显示次序必须是正整数").setPositiveButton("确定", null).show();
		    		 check=false;
				}							  
			     indexJO.put(Sort.SUPPLIER, Constant.supplierJO.getString(Supplier.TELNUM));
		              	
				  if(check){
				    result.setText("请稍后");
				    progres.setVisibility(View.VISIBLE);
					confirm.setEnabled(false);
					String[] params= new String[10];
	   	            params[0]=Constant.BaseUrl+Constant.SupplierUrl;
	                params[1]=affair; //PostCode
	               // params[2]=jsonData.toString();
	                params[2]=indexJO.toString();
	                new HttpPostTaskforIndexBasic().execute(params);
				  }else{
					result.setText("输入消息错误,无法执行");
				  }	
		}};		
	}
		
	private void init_component(View view){
		
	
		id=(EditText) view.findViewById(R.id.et_id_frg_indexbasic);
		name=(EditText) view.findViewById(R.id.et_name_frg_indexbasic);
		show_no=(EditText) view.findViewById(R.id.et_shownum_frg_indexbasic);
	
		confirm=(Button) view.findViewById(R.id.btn_confirm_frg_indexbasic);
	
		result=(TextView) view.findViewById(R.id.tv_functionresult_frg_indexbasic);
		progres= (ProgressBar) view.findViewById(R.id.prgs_frg_indexbasic);
		title=(TextView) view.findViewById(R.id.tv_mainttitle_frg_indexbasic);
		//////////////////////
		id.setOnKeyListener(null);
		id.setClickable(false);
		id.setFocusable(false);
		confirm.setOnClickListener(myOnclickListener);
		progres.setVisibility(View.INVISIBLE);
		
		
	}
	
	private void enable_all_infocomponent(boolean state){

		
		id.setEnabled(state);
		name.setEnabled(state);
	    show_no.setEnabled(state);
	
	}
	
	private void clear_all_infocomponet(){
	  
	
		id.setText("");
		name.setText("");
		show_no.setText("");
	
	}
	
	private  class HttpPostTaskforIndexBasic extends android.os.AsyncTask<String,String,Map<String,Object>>{

		

		@Override
		protected Map<String,Object> doInBackground(String... params) {
			// TODO Auto-generated method stub
			  Map<String,Object> map =	CommonFunction.UniversalHttpPost(params);		             
			return map;
		}
		
		@Override
		protected void onPostExecute(Map<String,Object> map) {
			// TODO Auto-generated method stub
		  //	super.onPostExecute(result);
			confirm.setEnabled(true);
			result.setText("");
			 progres.setVisibility(View.INVISIBLE);
			 String PostResult=(String) map.get("PostResult");
			 
			 boolean networkstate=(Boolean) map.get("PostState");
			 if(!networkstate){
				  result.setText("提示:网络故障或者网络配置有误，无法连接到服务器");
			    return;  
			}
				Toast.makeText(getActivity(), "服务器返回数据为"+PostResult, Toast.LENGTH_SHORT).show();
				Boolean success=false;
				String errorInfo="";
				JSONObject json = null;
				try{
			     json= JSONObject.fromObject(PostResult);
				 success=json.getBoolean("success");
				 errorInfo=json.getString("errorInfo");	
				}catch(JSONException e){
					errorInfo="JSON解析错误";
				}
				 result.setText("提示:"+errorInfo);
        						   												      		       
		       if(success){     	 
		    	   if(json.getString("affair").equals("createIndex")){
		    		   id.setText(json.getJSONObject("sort").getInt(Sort.ID)+"");	    	   
	                          }
		                } 
		  } //onPostExecute
 } //AsyncTask
}
